package wechat

import (
	"context"
	"errors"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PhoneNumberLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPhoneNumberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PhoneNumberLogic {
	return &PhoneNumberLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PhoneNumberLogic) PhoneNumber(req *types.Code) (resp *types.PhoneNumberRsp, err error) {
	phone, err := l.svcCtx.MiniApp.PhoneNumber.GetUserPhoneNumber(l.ctx, req.Code)
	if err != nil {
		return nil, err
	}
	if phone.ErrCode != 0 {
		return nil, errors.New(phone.ErrMsg)
	}

	return &types.PhoneNumberRsp{Phone: phone.PhoneInfo.PhoneNumber}, nil
}
